//
// Created by yoolo on 2020/11/8.
//

#ifndef LESSIONSIMPLE_SLPLAYER_H
#define LESSIONSIMPLE_SLPLAYER_H


#include <filesystem>
#include <SLES/OpenSLES.h>
#include <SLES/OpenSLES_Android.h>
#include <assert.h>

//engine interface
static SLObjectItf engineObject = NULL;
static SLEngineItf engineEngine = NULL;

//pcm audio player interface
static SLObjectItf playerObject = NULL;
static SLPlayItf playerPlay = NULL; //播放器对象
static SLObjectItf outputMixObjext = NULL; // 混音器
static SLAndroidSimpleBufferQueueItf playerBufferQueueItf = NULL;

#define NUM_RECORDER_EXPLICIT_INTERFACES 2
#define NUM_BUFFER_QUEUE 1
#define SAMPLE_RATE 44100
#define PERIOD_TIME 20  // 20ms
#define FRAME_SIZE SAMPLE_RATE * PERIOD_TIME / 1000
#define CHANNELS 2
#define BUFFER_SIZE   (FRAME_SIZE * CHANNELS)

class AudioContext {
public:
    FILE *pflie;
    uint8_t *buffer;
    size_t bufferSize;

    AudioContext(FILE *file, uint8_t *buf, size_t bufSize) {
        this->pflie = file;
        this->buffer = buf;
        this->bufferSize = bufSize;
    }

    ~AudioContext() {

    }
};

class SLPlayer {
private:
    const char *_path;
    void createEngine();

    void
    createAudioPlayer(SLEngineItf engineItf, SLObjectItf outputMixObject, SLObjectItf &audioObject);
public:
    SLPlayer(const char *p);

    ~SLPlayer();

    AudioContext *audioContext = NULL;

    void start();
    void stop();



};


#endif //LESSIONSIMPLE_SLPLAYER_H
